import { getHwCheckList, doSubmitCheck } from '@/api/bussiness/homework/hwCheck'

const state = {
  homework: {
    id: 9,
    courseId: 2,
    homeworkName: '实验一',
    startTime: '2023-04-21 00:00:00',
    endTime: '2023-04-30 00:00:00'
  },
  submitWithCheck: [ // 作业提交信息和批改信息
    {
      stuNo: '20190102',
      studentName: '李伊美',
      hasSubmit: true,
      submitInfo: {
        id: 1,
        stuNo: '20190102',
        studentName: '李伊美',
        homeworkId: 9,
        homeworkName: '实验一',
        createdTime: '2023-04-26 21:41:38',
        modifiedTime: '2023-04-26 21:41:38',
        questionSubmitList: [
          {
            id: 1,
            questionId: 19,
            questionType: 0,
            submitId: 1,
            content: '2',
            selectedOptions: [
              2
            ],
            blankAnswers: null
          },
          {
            id: 2,
            questionId: 20,
            questionType: 0,
            submitId: 1,
            content: '0,1,2,3',
            selectedOptions: [
              0,
              1,
              2,
              3
            ],
            blankAnswers: null
          },
          {
            id: 3,
            questionId: 10,
            questionType: 1,
            submitId: 1,
            content: '["9"]',
            selectedOptions: null,
            blankAnswers: [
              '9'
            ]
          },
          {
            id: 4,
            questionId: 11,
            questionType: 1,
            submitId: 1,
            content: '["望明月","思故乡哈哈"]',
            selectedOptions: null,
            blankAnswers: [
              '望明月',
              '思故乡哈哈'
            ]
          },
          {
            id: 5,
            questionId: 15,
            questionType: 2,
            submitId: 1,
            content: '实验一的报告和代码已发到邮箱，请查看',
            selectedOptions: null,
            blankAnswers: null
          }
        ]
      },
      hasChecked: true,
      checkInfo: {
        id: 1,
        submitId: 1,
        score: 8.9
      }
    },
    {
      stuNo: '20190103',
      studentName: '李哈哈',
      hasSubmit: false,
      submitInfo: null,
      hasChecked: false,
      checkInfo: null
    },
    {
      stuNo: '20190104',
      studentName: '王呵呵',
      hasSubmit: true,
      submitInfo: {
        id: 1,
        stuNo: '20190104',
        studentName: '王呵呵',
        homeworkId: 9,
        homeworkName: '实验一',
        createdTime: '2023-04-24 21:41:38',
        modifiedTime: '2023-04-25 21:41:38',
        questionSubmitList: []
      },
      hasChecked: false,
      checkInfo: null
    }
  ]
}

const mutations = {

}

const actions = {
  refresh({ state }, hwId) {
    return new Promise((resolve, reject) => {
      getHwCheckList(hwId).then(({ data }) => {
        state.homework = data.homework
        state.submitWithCheck = data.submitWithCheckList
        resolve(data)
      }).catch((err) => { reject(err) })
    })
  },
  doCheck({ state }, param) {
    return new Promise((resolve, reject) => {
      const { submitId, score } = param
      let aim = null
      let idx = null
      for (let i = 0; i < state.submitWithCheck.length; i++) {
        const sub = state.submitWithCheck[i]
        if (sub.hasSubmit && sub.submitInfo.id === submitId) {
          aim = sub
          idx = i
          break
        }
      }
      if (aim) {
        doSubmitCheck({ submitId, score }).then((data) => {
          if (aim.hasChecked) {
            state.submitWithCheck[idx].checkInfo.score = score
          } else {
            state.submitWithCheck[idx].hasChecked = true
            state.submitWithCheck[idx].checkInfo = {
              id: null,
              submitId: submitId,
              score: score
            }
          }
          resolve(data)
        }).catch((err) => reject(err))
      } else {
        reject()
      }
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
